package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ProductDetailConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.cskaoyan.shopping.constants.GlobalConstants.*;

/**
 * @author Tangyin
 * @description
 * @since 2022/05/19 19:53
 */
@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ProductDetailConverter productDetailConverter;


    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        try {
            Example example=new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("status",PRODUCT_ITEM_EXPIRE_TIME);
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto result = productDetailConverter.productDoToProductDto(item);
            productDetailResponse.setProductDetailDto(result);
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(productDetailResponse, e);
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse=new AllProductResponse();
        List<ProductDto> productDtoList= null;
        Integer page = request.getPage();
        Integer size = request.getSize();
        //String sort = request.getSort();
        PageHelper.startPage(page,size);
        try {
            //通过cid查询商品
            Example example=new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            /*Integer priceGt = request.getPriceGt();
            Integer priceLte = request.getPriceLte();
            if (priceLte!=null&&!priceLte.equals("")||priceGt!=null&&!priceGt.equals("")){
                example.getOrderByClause()
            }*/

            criteria.andEqualTo("cid",request.getCid());
            criteria.andEqualTo("status",1);
            List<Item> items = itemMapper.selectByExample(example);
            productDtoList = new ArrayList<>();
            for (Item item:items
            ) {
                ProductDto productDto = new ProductDto();
                productDto.setPicUrl(item.getImageBig());
                productDto.setProductId(item.getId());
                productDto.setProductName(item.getTitle());
                productDto.setSalePrice(item.getPrice());
                productDto.setSubTitle(item.getSellPoint());
                productDtoList.add(productDto);
            }
            allProductResponse.setProductDtoList(productDtoList);


            //PageInfo<ProductDto> productDtoPageInfo = new PageInfo<>(productDtoList);
            Example example1=new Example(Item.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("status",PRODUCT_CATE_EXPIRE_TIME);
            Long i = Long.valueOf(itemMapper.selectCountByExample(example1));
            allProductResponse.setTotal(i);
            allProductResponse.setProductDtoList(productDtoList);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(allProductResponse, e);
        }
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse recommendResponse = new RecommendResponse();
        try {
            Set<PanelDto> panelDtoSet = new HashSet<>();
            PanelDto panelDto = new PanelDto();
            List<PanelContentItemDto> panelContentItemDtoList = new ArrayList<>();
            Example example = new Example(PanelContent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("panelId", RECOMMEND_CACHE_EXPIRE);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            List<Panel> list = panelMapper.selectPanelContentById(RECOMMEND_CACHE_EXPIRE);
            Panel panel = list.get(0);
            panelDto.setName(panel.getName());
            panelDto.setSortOrder(panel.getSortOrder());
            panelDto.setType(panel.getType());
            panelDto.setStatus(panel.getStatus());
            panelDto.setRemark(panel.getRemark());
            panelDto.setPosition(panel.getPosition());
            panelDto.setLimitNum(panel.getLimitNum());
            panelDto.setId(panel.getId());



            for (PanelContent panelContent : panelContents
            ) {
                PanelContentItemDto panelContentItemDto = new PanelContentItemDto();
                Long productId = panelContent.getProductId();
                Example example1 = new Example(Item.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("id", productId);
                List<Item> items = itemMapper.selectByExample(example1);
                Item item = items.get(0);
                panelContentItemDto.setSalePrice(item.getPrice());
                panelContentItemDto.setPicUrl(panelContent.getPicUrl());
                panelContentItemDto.setPicUrl2(panelContent.getPicUrl2());
                panelContentItemDto.setPicUrl3(panelContent.getPicUrl3());
                panelContentItemDto.setFullUrl(panelContent.getFullUrl());
                panelContentItemDto.setCreated(panelContent.getCreated());
                panelContentItemDto.setType(panelContent.getType());
                panelContentItemDto.setUpdated(panelContent.getUpdated());
                panelContentItemDto.setProductName(item.getTitle());
                panelContentItemDto.setPanelId(panelContent.getPanelId());
                panelContentItemDto.setSubTitle(item.getSellPoint());
                panelContentItemDtoList.add(panelContentItemDto);

            }

            panelDto.setPanelContentItems(panelContentItemDtoList);
            panelDtoSet.add(panelDto);
            recommendResponse.setPanelContentItemDtos(panelDtoSet);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(recommendResponse, e);
        }
        return recommendResponse;
    }


    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}
